<?php
/*********************************************\
|* @author Yannici                           *|
|* @copyright Yannici                        *|
|*********************************************|
|* @since 30.07.2013                         *|
|* @release 02.08.2013                       *|
\******************************************* */

header ("Content-type: image/jpeg");

error_reporting(null);

$width = $_GET['w'];
$height = $_GET['h'];

if (!file_exists($_GET['f'])) {
    header ("Content-type: image/jpeg");
    $image = imagecreate($width, $height);
    imagejpeg($image);
    imagedestroy($image);
} else {
    $file_extension = strtolower(end(explode('.', $_GET['f'])));
    $halfsizeheight = round($height / 2);
    $halfsizewidth = round($width/2);

    switch ($file_extension) {
        case 'png':
            $image = imagecreatefrompng($_GET['f']);
            break;
        case 'gif':
            $image = imagecreatefromgif($_GET['f']);
            break;
        case 'jpg':
            $image = imagecreatefromjpeg($_GET['f']);
            break;
        case 'jpeg':
            $image = imagecreatefromjpeg($_GET['f']);
            
            break;
        default:
            $image = imagecreate($width, $height);
            break;
    }

    $image1 = imagecreatetruecolor($width, $height);
    $imagesize = getimagesize($_GET['f']);
    
    $middleheight = round($imagesize[1]/2);
    $middlewidth = round($imagesize[0]/2);
    
    $startheight = $middleheight-$halfsizeheight;
    $startwidth = $middlewidth-$halfsizewidth;
    
    if($imagesize[0] < $width+$startwidth) {
        $startwidth = 0;
        $width = $imagesize[0];
    }
    
    if($imagesize[1] < $height+$startheight) {
        $startheight = 0;
        $height = $imagesize[1];
    }
   
    //imagecopyresized($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)
    imagecopyresized($image1, $image, 0, 0, $startwidth, $startheight, $_GET['w'], $_GET['h'], $width, $height);
    imagejpeg($image1);
    imagedestroy($image1);
    imagedestroy($image);
}
?>
